using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Globalization;
namespace Fcdbas.Core
{
	[System.Diagnostics.DebuggerDisplay("{Value}")]
	[System.Serializable]
	public struct SalDateTime : System.IComparable, System.IConvertible, System.IFormattable, INullable, ISalType
	{
		[Browsable(false), System.Diagnostics.DebuggerStepThrough, System.Obsolete("Replaced by SalArray<T>.")]
		[System.Serializable]
		public class Array : SalArray<SalDateTime>
		{
			public Array()
			{
			}
			public Array(params int[] elems) : base(elems)
			{
			}
			public Array(params string[] elems) : base(elems)
			{
			}
			protected override object CreateInstance()
			{
				return SalDateTime.Null;
			}
		}
		internal const string DefaultFormat = "yyyy-MM-dd-HH.mm.ss.ffffff";
		public static readonly SalDateTime Null = new SalDateTime(System.DBNull.Value);
		private System.DateTime m_value;
		private bool m_isNotNull;
		public bool IsNull
		{
			[System.Diagnostics.DebuggerStepThrough]
			get
			{
				return !this.m_isNotNull;
			}
		}
		public System.DateTime Value
		{
			[System.Diagnostics.DebuggerStepThrough]
			get
			{
				return this.m_value;
			}
		}
		public long Ticks
		{
			[System.Diagnostics.DebuggerStepThrough]
			get
			{
				return this.m_value.Ticks;
			}
		}
		public static SalDateTime Current
		{
			get
			{
				return new SalDateTime(System.DateTime.Now);
			}
		}
		DbType ISalType.DbType
		{
			get
			{
				return DbType.DateTime;
			}
		}
		object ISalType.Value
		{
			get
			{
				if (this.IsNull)
				{
					return System.DBNull.Value;
				}
				return this.Value;
			}
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalDateTime(System.DateTime x)
		{
			this.m_value = x;
			this.m_isNotNull = true;
			this.m_isNotNull = true;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalDateTime(int year, int month, int day)
		{
			this = new SalDateTime(year, month, day, 0, 0, 0, 0);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalDateTime(int year, int month, int day, int hour, int minute, int second)
		{
			this = new SalDateTime(year, month, day, hour, minute, second, 0);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalDateTime(int year, int month, int day, int hour, int minute, int second, int milliseconds)
		{
			try
			{
				this.m_isNotNull = true;
				this.m_isNotNull = true;
				this.m_value = new System.DateTime(year, month, day, hour, minute, second, milliseconds);
			}
			catch (System.ArgumentOutOfRangeException)
			{
				try
				{
					System.DateTime value = new System.DateTime(year, 1, 1, 0, 0, 0, 0);
					value = value.AddMonths(month - 1);
					value = value.AddDays((double)(day - 1));
					double value2 = (double)((long)hour * 3600000L + (long)minute * 60000L + (long)second * 1000L + (long)milliseconds);
					value = value.AddMilliseconds(value2);
					this.m_value = value;
					this.m_isNotNull = true;
					this.m_isNotNull = true;
				}
				catch
				{
					this.m_isNotNull = false;
					this.m_isNotNull = false;
					this.m_value = System.DateTime.MinValue;
				}
			}
		}
		[System.Diagnostics.DebuggerStepThrough]
		private SalDateTime(System.DBNull n)
		{
			this.m_isNotNull = false;
			this.m_isNotNull = false;
			this.m_value = System.DateTime.MinValue;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public System.DateTime ToDateTime()
		{
			return this.m_value;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator System.DateTime(SalDateTime x)
		{
			return x.ToDateTime();
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator SalBoolean(SalDateTime x)
		{
			return !x.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator SalDateTime(System.DBNull n)
		{
			return SalDateTime.Null;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator SalDateTime(System.DateTime x)
		{
			return new SalDateTime(x);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator bool(SalDateTime x)
		{
			return !x.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator true(SalDateTime x)
		{
			return !x.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator false(SalDateTime x)
		{
			return x.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator !(SalDateTime x)
		{
			return x.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalDateTime AddDays(double days)
		{
			if (this.IsNull)
			{
				return SalDateTime.Null;
			}
			return new SalDateTime(this.m_value.AddDays(days));
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static SalDateTime operator +(SalDateTime x, SalNumber y)
		{
			return x.AddDays(y.ToDouble());
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static SalDateTime operator +(SalNumber x, SalDateTime y)
		{
			return y.AddDays(x.ToDouble());
		}
		[System.Diagnostics.DebuggerStepThrough]
		public double Subtract(SalDateTime x)
		{
			if (this.IsNull || x.IsNull)
			{
				return 0.0;
			}
			return this.m_value.Subtract(x.m_value).TotalDays;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static SalDateTime operator -(SalDateTime x, SalNumber y)
		{
			return x.AddDays(-y.ToDouble());
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static SalNumber operator -(SalDateTime x, SalDateTime y)
		{
			return x.Subtract(y);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static SalNumber operator -(SalDateTime x, System.DateTime y)
		{
			return x.Subtract(y);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static SalNumber operator -(System.DateTime x, SalDateTime y)
		{
			return x.Subtract(y).TotalDays;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static int Compare(SalDateTime x, SalDateTime y)
		{
			return System.DateTime.Compare(x.m_value, y.m_value);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator ==(SalDateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) == 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator ==(SalDateTime x, System.DateTime y)
		{
			return SalDateTime.Compare(x, y) == 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator ==(System.DateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) == 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator !=(SalDateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) != 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator !=(SalDateTime x, System.DateTime y)
		{
			return SalDateTime.Compare(x, y) != 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator !=(System.DateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) != 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator >(SalDateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) > 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator >(SalDateTime x, System.DateTime y)
		{
			return SalDateTime.Compare(x, y) > 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator >(System.DateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) > 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator <(SalDateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) < 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator <(SalDateTime x, System.DateTime y)
		{
			return SalDateTime.Compare(x, y) < 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator <(System.DateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) < 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator <=(SalDateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) <= 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator <=(SalDateTime x, System.DateTime y)
		{
			return SalDateTime.Compare(x, y) <= 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator <=(System.DateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) <= 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator >=(SalDateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) >= 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator >=(SalDateTime x, System.DateTime y)
		{
			return SalDateTime.Compare(x, y) >= 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator >=(System.DateTime x, SalDateTime y)
		{
			return SalDateTime.Compare(x, y) >= 0;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public override bool Equals(object obj)
		{
			if (obj is SalDateTime)
			{
				long arg_26_0 = this.m_value.Ticks;
				System.DateTime value = ((SalDateTime)obj).m_value;
				return arg_26_0 == value.Ticks;
			}
			return this.m_value.Equals(obj);
		}
		public override int GetHashCode()
		{
			return this.m_value.GetHashCode();
		}
		public SalNumber ToString(ref SalString target)
		{
			target = this.ToString();
			return target.Length;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public override string ToString()
		{
			return ((System.IConvertible)this).ToString(null);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalString ToString(string format)
		{
			return ((System.IFormattable)this).ToString(format, null);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalString ToString(string format, System.IFormatProvider provider)
		{
			return ((System.IFormattable)this).ToString(format, provider);
		}
		internal static string GetDefaultLongFormat()
		{
			System.Globalization.DateTimeFormatInfo currentInfo = System.Globalization.DateTimeFormatInfo.CurrentInfo;
			string shortDatePattern = currentInfo.ShortDatePattern;
			string text = currentInfo.LongTimePattern;
			int num = text.IndexOf("ss");
			if (num > -1 && (num + 2 <= text.Length || text[num + 2] == ' '))
			{
				text = text.Insert(num + 2, currentInfo.TimeSeparator + "ffffff");
			}
			return shortDatePattern + " " + text;
		}
		public SalNumber Day()
		{
			if (this.IsNull)
			{
				return SalNumber.MinusOne;
			}
			return this.m_value.Day;
		}
		public SalNumber Hour()
		{
			if (this.IsNull)
			{
				return SalNumber.MinusOne;
			}
			return this.m_value.Hour;
		}
		public SalNumber Minute()
		{
			if (this.IsNull)
			{
				return SalNumber.MinusOne;
			}
			return this.m_value.Minute;
		}
		public SalNumber Month()
		{
			if (this.IsNull)
			{
				return SalNumber.MinusOne;
			}
			return this.m_value.Month;
		}
		public SalDateTime MonthBegin()
		{
			if (this.IsNull)
			{
				return SalDateTime.Null;
			}
			return new System.DateTime(this.m_value.Year, this.m_value.Month, 1, 0, 0, 0, 0);
		}
		public SalNumber Quarter()
		{
			if (this.IsNull)
			{
				return SalNumber.MinusOne;
			}
			return (this.m_value.Month - 1) / 3 + 1;
		}
		public SalDateTime QuarterBegin()
		{
			if (this.IsNull)
			{
				return SalDateTime.Null;
			}
			int month = (this.m_value.Month - 1) / 3 * 3 + 1;
			int day = 1;
			return new System.DateTime(this.m_value.Year, month, day, 0, 0, 0, 0);
		}
		public SalNumber Second()
		{
			if (this.IsNull)
			{
				return SalNumber.MinusOne;
			}
			return this.m_value.Second;
		}
		public SalDateTime WeekBegin()
		{
			if (this.IsNull)
			{
				return SalDateTime.Null;
			}
			System.DateTime x = new System.DateTime(this.m_value.Year, this.m_value.Month, this.m_value.Day, 0, 0, 0, 0);
			switch (this.m_value.DayOfWeek)
			{
			case System.DayOfWeek.Sunday:
				x = x.Subtract(new System.TimeSpan(6, 0, 0, 0, 0));
				break;
			case System.DayOfWeek.Monday:
				return x;
			default:
				x = x.Subtract(new System.TimeSpan(this.m_value.DayOfWeek - System.DayOfWeek.Monday, 0, 0, 0, 0));
				break;
			}
			return x;
		}
		public SalNumber Weekday()
		{
			if (this.IsNull)
			{
				return SalNumber.MinusOne;
			}
			int num = (int)(this.m_value.DayOfWeek + 1);
			if (num > 6)
			{
				num = 0;
			}
			return num;
		}
		public SalNumber Year()
		{
			if (this.IsNull)
			{
				return SalNumber.MinusOne;
			}
			return this.m_value.Year;
		}
		public SalDateTime YearBegin()
		{
			if (this.IsNull)
			{
				return SalDateTime.Null;
			}
			return new System.DateTime(this.m_value.Year, 1, 1, 0, 0, 0, 0);
		}
		public int CompareTo(object obj)
		{
			if (obj is SalDateTime)
			{
				return SalDateTime.Compare(this, (SalDateTime)obj);
			}
			return this.m_value.CompareTo(obj);
		}
		public int CompareTo(SalDateTime x)
		{
			return SalDateTime.Compare(this, x);
		}
		ulong System.IConvertible.ToUInt64(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToUInt64(provider);
		}
		sbyte System.IConvertible.ToSByte(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToSByte(provider);
		}
		double System.IConvertible.ToDouble(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToDouble(provider);
		}
		System.DateTime System.IConvertible.ToDateTime(System.IFormatProvider provider)
		{
			return this.ToDateTime();
		}
		float System.IConvertible.ToSingle(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToSingle(provider);
		}
		bool System.IConvertible.ToBoolean(System.IFormatProvider provider)
		{
			return !this.IsNull;
		}
		int System.IConvertible.ToInt32(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToInt32(provider);
		}
		ushort System.IConvertible.ToUInt16(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToUInt16(provider);
		}
		short System.IConvertible.ToInt16(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToInt16(provider);
		}
		byte System.IConvertible.ToByte(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToByte(provider);
		}
		char System.IConvertible.ToChar(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToChar(provider);
		}
		long System.IConvertible.ToInt64(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToInt64(provider);
		}
		System.TypeCode System.IConvertible.GetTypeCode()
		{
			return System.TypeCode.DateTime;
		}
		decimal System.IConvertible.ToDecimal(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToDecimal(provider);
		}
		object System.IConvertible.ToType(System.Type targetType, System.IFormatProvider provider)
		{
			if (targetType == typeof(SalDateTime))
			{
				return this;
			}
			return ((System.IConvertible)this.m_value).ToType(targetType, provider);
		}
		uint System.IConvertible.ToUInt32(System.IFormatProvider provider)
		{
			return ((System.IConvertible)this.m_value).ToUInt32(provider);
		}
		string System.IConvertible.ToString(System.IFormatProvider provider)
		{
			return ((System.IFormattable)this).ToString("yyyy-MM-dd-HH.mm.ss.ffffff", provider);
		}
		string System.IFormattable.ToString(string format, System.IFormatProvider provider)
		{
			if (!this.IsNull)
			{
				try
				{
					if (format != null && format.Length > 0)
					{
						if (format.Length == 1)
						{
							if (format == "S")
							{
								format = "d";
							}
							else
							{
								if (format == "L")
								{
									format = "D";
								}
							}
						}
						else
						{
							if (format.Length > 1)
							{
								format = format.Replace("hhhh", "HH");
								format = format.Replace("AMPM", "tt");
								format = format.Replace("mmmmmm", "ffffff");
							}
						}
					}
					else
					{
						format = SalDateTime.GetDefaultLongFormat();
					}
					return this.m_value.ToString(format, provider);
				}
				catch (System.FormatException)
				{
				}
			}
			return string.Empty;
		}
	}
}
